import {SONG_URL,LRC,ALBUM_IMG,VKEY} from '../api'
export default (state,action)=>{
  state = state || {
    songUrl:sessionStorage.getItem('songUrl',action.songUrl) || '',
    list:JSON.parse(sessionStorage.getItem('songList')) || [],
    index:JSON.parse(sessionStorage.getItem('index')) || -1,
    currentSong:JSON.parse(sessionStorage.getItem('currentSong')) || {},
    albumImgUrl:sessionStorage.getItem('albumImgUrl') || '',
    singerAvatarUrl:sessionStorage.getItem('singerAvatarUrl') || '',
    vkey:'',
    lyric:JSON.parse(sessionStorage.getItem('lyric')) || []
  };
  switch(action.type){
    case 'SONG_URL':
      sessionStorage.setItem('songUrl',action.songUrl);
      return {...state,songUrl:action.songUrl}
    case 'PLAYER_SONG_LIST':
      sessionStorage.setItem('songList',JSON.stringify(action.songList));
      return {...state,list:action.songList};
    case 'SONG_IMG':
      sessionStorage.setItem('albumImgUrl',action.albumImgUrl);
      sessionStorage.setItem('singerAvatarUrl',action.singerAvatarUrl);
      return {...state,albumImgUrl:action.albumImgUrl,singerAvatarUrl:action.singerAvatarUrl}
    case 'V_KEY':
      return {...state,vkey:action.vkey}
    case 'CURRENT_SONG':
      const index = state.list.findIndex(s=>{
        return s.songMid === action.songId
      })
      sessionStorage.setItem('currentSong',JSON.stringify(state.list[index]));
      sessionStorage.setItem('index',index);
      return {...state,index,currentSong:state.list[index]};
    case 'PREV_SONG':
      var prevIndex = state.index>0 ? state.index - 1 : 0;
      var prevSong = state.list[prevIndex];
      return {...state,index:prevIndex,currentSong:prevSong};
    case 'NEXT_SONG':
      var nextIndex = state.index === state.list.length-1 ? 0 : state.index+1;
      var nextSong = state.list[nextIndex];
      return {...state,index:nextIndex,currentSong:nextSong};
    case 'LRC':
      sessionStorage.setItem('lyric',JSON.stringify(action.lyric));
      return {...state,lyric:action.lyric};
    default:
      return state;
  }
}
export const setCurrentSong=(songId)=>(dispatch)=>{
  dispatch({type:'CURRENT_SONG',songId});
}

export const setSongList=(list)=>(dispatch)=>{
  dispatch({type:'PLAYER_SONG_LIST',songList:list});
}

// ,{
//   loginUin:3051522991,
//   format:'jsonp',
//   platform:'yqq',
//   needNewCode:0,
//   cid:205361747,
//   uin:3051522991,
//   guid:5931742855,
//   songmid:songMid,
//   filename:'C400'+songMid+'.m4a'
// }
export const getAudioVKey=(songMid)=>(dispatch)=>{
    fetch(`${VKEY}?loginUin=3051522991&format=jsonp&platform=yqq&needNewCode=0&cid=205361747&uin=3051522991&guid=5931742855&songmid=${songMid}&filename=C400${songMid}.m4a`).then(data=>data.json()).then(res=>{
      dispatch({type:'V_KEY',vkey:res.data.items[0].vkey});
    })
}

export const getPlayerInfo=(songid,vkey)=>(dispatch)=>{
  // 歌曲播放地址
  // fetch(`${SONG}${songMid}.m4a?fromtag=38&guid=5931742855&vkey=${vkey}`).then(data=>data.json()).then(res=>{
  //   dispatch({type:'SONG_URL',songUrl:res.data[0]})
  // })
  fetch(`${SONG_URL}/${songid}`).then(data=>data.json()).then(res=>{
    dispatch({type:'SONG_URL',songUrl:res.data[0]})
  })
}

export const getLyric = (songId)=>(dispatch)=>{
  // 歌词
  fetch(LRC+'/'+songId).then(data=>data.json()).then(res=>{
    let arr2=[];
    let arr = res.data.lyric.split('[换行]')
    for (let i in arr) {
      let s = arr[i]
      let endIndex = s.indexOf(']')
      let time = s.substring(0, endIndex + 1).replace('[', '').replace(']', '')
      // 将时间格式转换为秒才存储方便后面作对比
      let min = parseInt(time.split(':')[0]) * 60
      let sec = parseFloat(time.split(':')[1])
      // 截取歌词内容
      let lrc = s.substring(endIndex + 1, s.length)
      if (lrc.trim().length > 0){
        arr2.push({
          time: (min + sec).toFixed(2), lrc
        })
      }
    }
    dispatch({type:'LRC',lyric:arr2});
  })
}

export const prevSong = ()=>(dispatch)=>{
  dispatch({type:'PREV_SONG'});
}

export const nextSong = ()=>(dispatch)=>{
  dispatch({type:'NEXT_SONG'});
}

//albummid:专辑ID    singerMid:歌手ID
export const getImg=(albummid,singerMid)=>(dispatch)=>{
  // 歌曲封面和歌手封面
  fetch(ALBUM_IMG+'/'+albummid+'/'+singerMid).then(data=>data.json()).then(res=>{
    dispatch({type:'SONG_IMG',albumImgUrl:res.data.albumImgUrl,singerAvatarUrl:res.data.singerAvatarUrl})
  })
}